/*
 * Copyright (c) 2012, Marcin Czekaj
 * All rights reserved.
 *
 * This software is open-source under the BSD license; see LICENSE.TXT for a description.
 */
package pl.edu.uj.fais.controller;

import java.awt.geom.Point2D;
import java.util.Collection;

import pl.edu.uj.fais.graph.elements.Link;
import pl.edu.uj.fais.graph.elements.Node;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.graph.Graph;

public class ProxyController {
    
    private static Controller controller = Controller.getInstance();
    
    public static void centerProxyNodes() {
        
        Graph<Node, Link> graph = controller.getGraph();
        CircleLayout<Node, Link> layout = controller.getLayout();
        
        for (Node node : graph.getVertices()) {
            if (node.isProxyNode()) {
                
                double x = 0.0;
                double y = 0.0;
                
                Collection<Node> neighbors = graph.getNeighbors(node);
                for (Node neighbor : neighbors) {
                    x += layout.getX(neighbor);
                    y += layout.getY(neighbor);
                }
                
                int size = neighbors.size();
                
                Point2D.Double center = new Point2D.Double(x / size, y / size);
                layout.setLocation(node, center);
            }
        }
    }
    
    public static void centerProxyNode(Node proxy, Node start, Node end) {
        
        CircleLayout<Node, Link> layout = controller.getLayout();
        
        double x = (layout.getX(start) + layout.getX(end)) / 2.0;
        double y = (layout.getY(start) + layout.getY(end)) / 2.0;
        
        layout.setLocation(proxy, new Point2D.Double(x, y));
    }
}
